import isArrayLike from '../util/isArrayLike'
import callback from '../function/callback'

/**
 * 迭代生成一个新数组
 * @param {Array|Object} obj
 * @param {Function} iteratee
 * @param {Object} context
 */
export function map(obj, iteratee, context) {
  iteratee = callback(iteratee, context)
  const keys = !isArrayLike(obj) && Object.keys(obj)
  const length = (keys || obj).length
  const result = Array(length)

  for (let i = 0; i < length; ++i) {
    const currentKey = keys ? keys[i] : i
    result[i] = iteratee(obj[currentKey], currentKey, obj)
  }

  return result
}
